import net_data as data
from net_train import Net
import torch

def test():
    net = Net()
    sd = torch.load("./model/net.pt")
    net.load_state_dict(sd)
    dl = data.testloader()
    # print(dl[0])
    correct = 0
    total = 0

    for step,d in enumerate(dl):
        out = net(d["data"])
        numbers = d["label"].numpy()

        # print("input numbers:",numbers)

        labels = data.get_labels()
        print("")
        print("input labels:")
        for n in numbers:
            print(chr(labels[n]),end=",")
        print()

        # print(out[0, :].detach().numpy().flatten().sum())
        max_index = torch.argmax(out,keepdim=True,dim=1)
        max_arr = max_index.numpy().flatten()

        # print("predict numbers:",max_arr)
        print("predict labels:")
        for n in max_arr:
            print(chr(labels[n]), end=",")

        print()

        label = d["label"].reshape(-1,1)

        eq = label.eq(max_index).int()
        correct += eq.sum().item()
        total += len(numbers)

    print("\n{}% on {} character test".format(correct*100/total,total))

if(__name__=="__main__"):
    test()